/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

/**
 * A helper class to make getting the current date and time easier
 * @author nknowles
 */
public class DateUtils {

  public static final String DATE_FORMAT = "yyyy-MM-dd";
  public static final String TIME_FORMAT = "HH:mm:ss";
  public static final String DATE_TIME_FORMAT = DATE_FORMAT + " " + TIME_FORMAT;
  public static final String DAY_FORMAT = "EEE";
  private static GregorianCalendar cal = null;

  public static String currentDate() {
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
    return sdf.format(cal.getTime());
  }

  public static String currentTime() {
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat(TIME_FORMAT);
    return sdf.format(cal.getTime());
  }

  public static String currentDateTime() {
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat sdf = new SimpleDateFormat(DATE_TIME_FORMAT);
    return sdf.format(cal.getTime());
  }

  public static String currentDay() {
    if (cal == null) {
      cal = (GregorianCalendar) GregorianCalendar.getInstance();
    }
    SimpleDateFormat sdf = new SimpleDateFormat(DAY_FORMAT);
    return sdf.format(cal.getTime());
  }

  public static String getDay(int year, int month, int date) {
    //TODO finish getting the day
    GregorianCalendar cal = new GregorianCalendar();
    year += cal.get(Calendar.YEAR);
    cal.set(year, month, date);
    SimpleDateFormat sdf = new SimpleDateFormat(DAY_FORMAT);
    return sdf.format(cal.getTime());
  }

  public static long timeInMilli() {
    return Calendar.getInstance().getTimeInMillis();
  }

  public static Date makeSqlDate(String date) {
    String[] splitDate = date.split("-");
    GregorianCalendar formatdate = null;
    switch (splitDate.length) {
      case 3:
        formatdate = new GregorianCalendar(Integer.parseInt(splitDate[0]), Integer.parseInt(splitDate[1]) - 1, Integer.parseInt(splitDate[2]));
        break;
      case 5:
        formatdate = new GregorianCalendar(Integer.parseInt(splitDate[0]), Integer.parseInt(splitDate[1]) - 1, Integer.parseInt(splitDate[2]), Integer.parseInt(splitDate[3]), Integer.parseInt(splitDate[4]));

    }
    Date time = new Date(formatdate.getTimeInMillis());
    return time;
  }

  public static void setCal(int year, int month, int date) {
    year += Calendar.getInstance().get(Calendar.YEAR);
    cal = new GregorianCalendar(year, month, date);
  }
  
  public static boolean matchCurrentDate(Date date) {
      Calendar currentDate = Calendar.getInstance();
      Calendar compareDate = Calendar.getInstance();
      compareDate.setTime(date);
      return (
              currentDate.get(Calendar.DATE)  == compareDate.get(Calendar.DATE) &&
              currentDate.get(Calendar.MONTH) == compareDate.get(Calendar.MONTH) &&
              currentDate.get(Calendar.YEAR)  == compareDate.get(Calendar.YEAR)
              
              );
  }
}
